package com.objectdraw.server.commands;

import com.objectdraw.server.CommunicationServer;
import com.objectdraw.server.ObjectDrawServer;
import com.objectdraw.server.data.RequestData;
import com.objectdraw.server.data.ResponseData;
import com.objectdraw.server.tools.data.DrawingObject;

/**
 * Processes a fullupdate command from the client.
 * Also handles clear commands.
 * 
 * @author jgulik, cfruehan, Harrison
 *
 */
public class FullUpdateCommand extends Command {
	static final private String myCommand = "FullUpdate";
	
	/**
	 * Takes a message with a fullupdate command and responds by sending a fullupdate message to the requesting client.
	 * Also, if a clear command is sent a "fullupdate end" message is broadcast to all listening clients.
	 */
	@Override
	public void processRequest(RequestData request, ResponseData response) {
		if((myCommand.equalsIgnoreCase(request.getCommand()) || request.getCommand().equalsIgnoreCase("connect"))){
			System.out.println(myCommand +" Command");
			ObjectDrawServer server = ObjectDrawServer.getInstance();
			
			ResponseData res = new ResponseData();
			res.addResponse("fullupdate");
			for(DrawingObject doo : server.getCanvasObjects()){
				res.addResponse(doo.toString());
			}
			res.addResponse("end");
			//Send full update to client;
			CommunicationServer.broadcast(res, request.getClient());
		}else if("clear".equalsIgnoreCase(request.getCommand())){
			ObjectDrawServer server = ObjectDrawServer.getInstance();
			server.removeAllObjects();
			//Broadcast the new thing
			response.addResponse("fullupdate end");
			CommunicationServer.broadcast(response, ObjectDrawServer.getInstance().getViewingClientSockets());
		}
		
		//Pass the request on to the next person in line
		if(this.successor != null){
			this.successor.processRequest(request,response);
		}
	}

}
